﻿using System;
using System.Windows;
using System.Windows.Input;

namespace ZhaGanSharp.Windows
{
    /// <summary>
    /// MusicPlayer.xaml 的交互逻辑
    /// </summary>
    public partial class MusicPlayer : Window
    {
        public MusicPlayer()
        {
            InitializeComponent();
            this.Title = Properties.Resources.AppName +" "+ MainWindow.strVer + " - MusicPlayer";
            this.TextBlock_Title.Text = Properties.Resources.AppName + " " + MainWindow.strVer + " - MusicPlayer";
            GetPosition();
            this.MediaElement_MusicPlayer.Play();
            is_Playing = true;
        }

        #region 取播放进度
        System.Threading.Thread GetPositionThread;
        private void GetPosition()
        {
            GetPositionThread = new System.Threading.Thread(GetPositionThread_DoWork);
            GetPositionThread.Name = "ChildThread_GetPosition";
            GetPositionThread.Priority = System.Threading.ThreadPriority.Normal;
            GetPositionThread.IsBackground = true;
            GetPositionThread.Start();
        }

        private void GetPositionThread_DoWork() {
            while (true)
            {
                System.Threading.Thread.Sleep(100);
                this.Dispatcher.Invoke(
                    System.Windows.Threading.DispatcherPriority.Background,
                    new Action(delegate{
                        //this.TextBlock_PlayDuration.Text = "播放进度: " + this.MediaElement_MusicPlayer.Position.ToString().Substring(0,8) + " / " + this.MediaElement_MusicPlayer.NaturalDuration.ToString().Substring(0,8);
                        this.TextBlock_PlayDuration.Text = "播放进度: " + this.MediaElement_MusicPlayer.Position.ToString() + " / " + this.MediaElement_MusicPlayer.NaturalDuration.ToString();
                        if (this.MediaElement_MusicPlayer.NaturalDuration.HasTimeSpan) { 
                            this.Slider_PlayPosition.Value = (this.MediaElement_MusicPlayer.Position.TotalMilliseconds / this.MediaElement_MusicPlayer.NaturalDuration.TimeSpan.TotalMilliseconds)*100;
                        }
                    })
                );
            }
        }
        #endregion

        #region 开始播放按钮
        Boolean is_Playing = false;
        Boolean is_Canvas_PlayButton_Pressed = false;
        private void Canvas_PlayButton_MouseEnter(object sender, MouseEventArgs e)
        {
            
        }

        private void Canvas_PlayButton_MouseLeave(object sender, MouseEventArgs e)
        {
            is_Canvas_PlayButton_Pressed = false;
        }

        private void Canvas_PlayButton_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            is_Canvas_PlayButton_Pressed = true;
        }

        private void Canvas_PlayButton_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (is_Canvas_PlayButton_Pressed && !is_Playing)
            {
                this.MediaElement_MusicPlayer.Play();
                is_Playing = true;
            }
        }
        #endregion

        #region 关闭窗口按钮
        Boolean is_Canvas_CloseButton_Pressed = false;
        private void Canvas_CloseButton_MouseEnter(object sender, MouseEventArgs e)
        {

        }

        private void Canvas_CloseButton_MouseLeave(object sender, MouseEventArgs e)
        {
            is_Canvas_CloseButton_Pressed = false;
        }

        private void Canvas_CloseButton_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            is_Canvas_CloseButton_Pressed = true;
        }

        private void Canvas_CloseButton_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (is_Canvas_CloseButton_Pressed) { this.Close(); }
        }
        #endregion

        #region 暂停播放按钮
        Boolean is_Canvas_PauseButton_Pressed = false;
        private void Canvas_PauseButton_MouseEnter(object sender, MouseEventArgs e)
        {

        }

        private void Canvas_PauseButton_MouseLeave(object sender, MouseEventArgs e)
        {
            is_Canvas_PauseButton_Pressed = false;
        }

        private void Canvas_PauseButton_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            is_Canvas_PauseButton_Pressed = true;
        }

        private void Canvas_PauseButton_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (is_Canvas_PauseButton_Pressed&&is_Playing) {
                this.MediaElement_MusicPlayer.Pause();
                is_Playing = false;
            }
        }
        #endregion

        #region 其他按钮
        Boolean is_Canvas_HelpButton_Pressed = false;
        private void Canvas_HelpButton_MouseEnter(object sender, MouseEventArgs e)
        {

        }

        private void Canvas_HelpButton_MouseLeave(object sender, MouseEventArgs e)
        {
            is_Canvas_HelpButton_Pressed = false;
        }

        private void Canvas_HelpButton_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            is_Canvas_HelpButton_Pressed = true;
        }

        private void Canvas_HelpButton_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (is_Canvas_HelpButton_Pressed) { MessageBox.Show("凑空间用的按钮,没实际作用",Properties.Resources.AppName,MessageBoxButton.OK,MessageBoxImage.Information); }
        }
        #endregion

        private void Canvas_Title_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }

        #region 进度条
        private void Slider_PlayPosition_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if (this.Slider_PlayPosition.IsMouseOver && Mouse.LeftButton == MouseButtonState.Pressed)
            {
                this.MediaElement_MusicPlayer.Position = TimeSpan.FromMilliseconds((this.Slider_PlayPosition.Value / 100) * this.MediaElement_MusicPlayer.NaturalDuration.TimeSpan.TotalMilliseconds);
            }
        }
        #endregion

        
    }
}
